import React, { Component } from 'react'
import { Card, Form, Input, Button, Checkbox, message } from 'antd'
import logo from 'assets/logo.png'
import { login } from 'api/user'
import style from './index.module.scss'
import { setToken } from 'utils/storage'
// 登录页面
export default class Login extends Component {
  state = {
    // 加载状态
    loading: false,
  }
  render() {
    return (
      <div className={style.login}>
        <Card className="login-container">
          {/* logo */}
          <img src={logo} alt="" className="login-logo" />
          {/* 表单 */}
          <Form
            size="large"
            validateTrigger={['onChange', 'onBlur']}
            onFinish={this.onFinish}
            initialValues={{
              mobile: '13911111111',
              code: '246810',
              agree: true,
            }}
          >
            {/* 手机号 */}
            <Form.Item
              name="mobile"
              rules={[
                { required: true, message: '请输入手机号' },
                { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' },
              ]}
            >
              <Input placeholder="请输入手机号" autoComplete="off" />
            </Form.Item>
            {/* 验证码 */}
            <Form.Item
              name="code"
              validateTrigger={['onChange', 'onBlur']}
              rules={[
                { required: true, message: '验证码不弄为空' },
                { pattern: /^\d{6}$/, message: '验证码格式不正确' },
              ]}
            >
              <Input placeholder="请输入验证码" autoComplete="off" />
            </Form.Item>
            {/* 同意勾选 */}
            <Form.Item
              name="agree"
              valuePropName="checked"
              rules={[
                {
                  validator(rules, val) {
                    if (val) {
                      return Promise.resolve()
                    } else {
                      return Promise.reject(new Error('请阅读并同意'))
                    }
                  },
                },
              ]}
            >
              <Checkbox>我已阅读并同意[条款]和[协议]</Checkbox>
            </Form.Item>
            {/* 登录按钮 */}
            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                block
                loading={this.state.loading}
              >
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }
  // 获取表单数据,发起登录请求
  onFinish = async ({ mobile, code }) => {
    this.setState({
      loading: true,
    })
    try {
      const { data: res } = await login(mobile, code)
      setToken(res.token)
      // 判断location是否有值,如果有则跳转到对应页面
      const { state } = this.props.location
      if (state) {
        this.props.history.push(state.from)
      } else {
        this.props.history.push('/home')
      }
      message.success('登录成功', 1)
    } catch (err) {
      message.warning(err.response.data.message, 1, () => {
        this.setState({
          loading: false,
        })
      })
    }
  }
}
